package com.house.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.house.common.constant.UserConstants;
import com.house.common.core.domain.entity.House;
import com.house.common.core.domain.entity.User;
import com.house.common.utils.StringUtils;
import com.house.system.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.house.system.mapper.MediumMapper;
import com.house.system.domain.Medium;
import com.house.system.service.IMediumService;

/**
 * 房屋中介表Service业务层处理
 *
 * @author tc
 * @date 2022-02-04
 */
@Service
public class MediumServiceImpl implements IMediumService {
    @Autowired
    private MediumMapper mediumMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询房屋中介表
     *
     * @param userId 房屋中介表主键
     * @return 房屋中介表
     */
    public List<Medium> selectMediumByUserId(Long userId) {
        return mediumMapper.selectMediumByUserId(userId);
    }

    /**
     * 查询房屋中介表列表
     *
     * @param houseId 房屋编号
     * @return 房屋中介表
     */
    @Override
    public List<Medium> selectMediumList(Long houseId, Long userId) {

        //这两个参数只有一个不为null,houseId为null时为查询,userId为null为页面初始化数据加载

        //获取所有的房屋中介对象
        List<User> users = userMapper.selectUserByRolekey("medium");

        //要返回的集合
        List<Medium> mediums = new ArrayList<>();

        //如果houseId不为null
        if (StringUtils.isNotNull(houseId)) {
            //获取该房屋编号负责的房屋中介对象
            Medium medium = mediumMapper.selectMeidumByHouseId(houseId);

            for (User user : users) {
                Medium m = new Medium();
                m.setUserId(user.getUserId());
                m.setUsername(user.getUserName());
                if (StringUtils.isNotNull(medium) && user.getUserId().longValue() == medium.getUserId().longValue()) {
                    //如果该房屋已有负责人,则设置该负责人flag为true
                    m.setFlag(true);
                }
                mediums.add(m);
            }
        } else if (StringUtils.isNotNull(userId)) {
            //如果userId不为null
            for (User user : users) {
                if (user.getUserId().longValue() == userId.longValue()) {
                    Medium m = new Medium();
                    m.setUserId(user.getUserId());
                    m.setUsername(user.getUserName());
                    mediums.add(m);
                }
            }
        } else {
            //如果都为null,则直接设置所有的房屋中介
            for (User user : users) {
                Medium m = new Medium();
                m.setUserId(user.getUserId());
                m.setUsername(user.getUserName());
                mediums.add(m);
            }
        }

        return mediums;
    }

    /**
     * 查询房屋中介列表
     *
     * @param medium 房屋中介
     * @return 房屋中介
     */
    @Override
    public List<Medium> selectMediumList(Medium medium) {
        return selectMediumList(medium.getHouseId(), medium.getUserId());
    }

    /**
     * 新增房屋中介表
     *
     * @param medium 房屋中介表
     * @return 结果
     */
    @Override
    public int insertMedium(Medium medium) {
        return mediumMapper.insertMedium(medium);
    }

    /**
     * 修改房屋中介表
     *
     * @param medium 房屋中介表
     * @return 结果
     */
    @Override
    public int updateMedium(Medium medium) {
        mediumMapper.deleteMediumByHouseId(medium.getHouseId());
        return mediumMapper.insertMedium(medium);
    }

    /**
     * 批量删除房屋中介表
     *
     * @param userIds 需要删除的房屋中介表主键
     * @return 结果
     */
    @Override
    public int deleteMediumByUserIds(Long[] userIds) {
        return mediumMapper.deleteMediumByUserIds(userIds);
    }

    /**
     * 删除房屋中介表信息
     *
     * @param userId 房屋中介表主键
     * @return 结果
     */
    @Override
    public int deleteMediumByUserId(Long userId) {
        return mediumMapper.deleteMediumByUserId(userId);
    }

    /**
     * 查询房屋中介表信息
     *
     * @param houseId 房屋编号
     * @return 结果
     */
    @Override
    public Medium selectMediumByHouseId(Long houseId) {
        return mediumMapper.selectMeidumByHouseId(houseId);
    }

    /**
     * 中介上传房产证
     *
     * @param houseId 房屋编号
     * @param estate 房产证地址
     * @return 结果
     */
    @Override
    public boolean updateMediumEstate(Long houseId, String estate) {
        Medium medium = new Medium();
        medium.setHouseId(houseId);
        medium.setEstate(estate);
        return mediumMapper.updateMedium(medium) > 0;
    }
}
